package com.mxwl.cbg.common.calculator;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.mxwl.cbg.common.model.source.role.HugeHorse;
import com.mxwl.cbg.common.model.source.role.RoleClothe;
import com.mxwl.cbg.common.model.source.role.XiangRui;
import com.mxwl.cbg.common.utils.JsonSafeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RoleCalculator {
    private final static Map<String, String> PROP_KEPT_KEYS = Map.of(
            "iStr", "str", "iMag", "mag", "iSpe", "spe", "iCor", "cor", "iRes", "res"
    );
    private final static List<String> PROP_KEPT_KEYS_ORDER = List.of("iStr", "iMag", "iSpe", "iCor", "iRes");


    /**
     * 计算靓号级别
     *
     * @param idbid_desc
     * @return
     */
    public static String calculateBeautyLevel(List<Integer> idbid_desc, Integer bid) {
        if (idbid_desc == null || idbid_desc.isEmpty()) {
            return null;
        }
        boolean is_xyq_id = false;
        //土豪金
        boolean is_gold_id = false;
        for (Integer id : idbid_desc) {
            if (id >= 85 && id <= 89) {
                is_xyq_id = true;
            }
            if (id > 5 && id <= 8) {
                is_gold_id = true;
            }
        }
        String is_niceid = null;
        if (is_xyq_id) {
            is_niceid = "梦幻西游";
        } else if (is_gold_id) {
            is_niceid = "土豪金";
        } else if (bid == null) {
            is_niceid = "未知";
        } else {
            is_niceid = bid == 1 ? "是" : "否";
        }
        return is_niceid;
    }

    /**
     * 处理祥瑞
     * 主要是拆分祥瑞分成普通和限量
     *
     * @param gameConfig
     * @param hugeHorseList
     * @return
     */
    public static XiangRui processingXiangRui(Map<String, Object> gameConfig, Map<String, HugeHorse> hugeHorseList) {
        //所有的祥瑞
        Map<String, String> all_xiangrui_info = (Map<String, String>) gameConfig.get("xiangrui_info");
        //限量祥瑞
        Map<String, String> nosale_xiangrui = (Map<String, String>) gameConfig.get("nosale_xiangrui");
        //
        Map<String, String> nosale_to_sale_xiangrui = (Map<String, String>) gameConfig.get("nosale_to_sale_xiangrui");
        //祥瑞技能
        Map<String, String> all_skills = (Map<String, String>) gameConfig.get("xiangrui_skill");

        if (hugeHorseList == null || hugeHorseList.isEmpty()) {
            return null;
        }

        XiangRui xiangRui = new XiangRui();
        hugeHorseList.forEach((key, hugeHorse) -> {
            hugeHorse.setKey(Integer.valueOf(key));
            boolean nosale = false;
            //首先排除肯定不是限量的祥瑞
            if (!nosale_to_sale_xiangrui.containsKey(String.valueOf(key))) {
                //再看祥瑞本身数据有没有标识是限量
                nosale = hugeHorse.getNosale() != null && hugeHorse.getNosale() == 1;
                if (!nosale) {
                    //如果祥瑞数据本身不是限量,就看在不在限量列表
                    nosale = nosale_xiangrui.containsKey(String.valueOf(key));
                }
            }
            if (nosale) {
                xiangRui.addNosale(hugeHorse);
            } else {
                xiangRui.addNormals(hugeHorse);
            }
        });
        return xiangRui;
    }

    /**
     * 获取属性加点方案
     *
     * @param propKept
     * @param grade
     * @return
     */
    public static List<String> get_prop_kept(Map<String, Map<String, Integer>> propKept, int grade) {
        List<String> res = new ArrayList<>();
        if (propKept != null && !propKept.isEmpty()) {
            propKept.forEach((k, v) -> {
                String s = parse_single_prop_kept(v, grade);
                if (s != null) {
                    res.add(s);
                }
            });
        }
        return res;
    }

    private static String parse_single_prop_kept(Map<String, Integer> prop, int grade) {
        if (prop == null || prop.isEmpty()) {
            return null;
        }
        List<Attribute> attr_list = new ArrayList<>();
        for (String key : PROP_KEPT_KEYS_ORDER) {
            if (prop.get(key) != null && prop.get(key) >= (grade * 2 + 10)) {
                attr_list.add(new Attribute(key, prop.get(key), PROP_KEPT_KEYS.get(key)));
            }
        }
        if (attr_list.isEmpty()) {
            return null;
        }
        if (attr_list.size() < 2) {
            return attr_list.get(0).name;
        }
        attr_list.sort((a, b) -> {
            //安装属性值降序,如果相同则按字典排序(极限)
            if (a.value == b.value) {
                return a.name.compareTo(b.name);
            } else {
                return b.value - a.value;
            }
        });
        //体耐:corres
        return (attr_list.get(0).name + attr_list.get(1).name).toLowerCase();
    }

    // 辅助内部类存储属性三元组
    private static class Attribute {
        String key;
        int value;
        String name;

        Attribute(String key, int value, String name) {
            this.key = key;
            this.value = value;
            this.name = name;
        }
    }
}
